package com.prtek.reminder.helper.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.prtek.reminder.helper.UpgradeHelper;
import com.prtek.reminder.model.ReminderDaoImpl;

/**
 * Created by jarly on 2018/9/14.
 */

public class RemindProvider extends ContentProvider {

  static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

  public static final int CODE_NOTES = 0x1;

  public static final int CODE_NOTESJOINUSER = 0x2;

  public static final int CODE_USER = 0x3;

  public static final String NOTES = "NOTES";

  public static final String NOTES_JOIN_USER = "NOTES_JOIN_USER";

  public static final String USER = "USER";

  static {
    uriMatcher.addURI(UpgradeHelper.AUTHORITY, NOTES, CODE_NOTES);
    uriMatcher.addURI(UpgradeHelper.AUTHORITY, NOTES_JOIN_USER, CODE_NOTESJOINUSER);
    uriMatcher.addURI(UpgradeHelper.AUTHORITY, USER, CODE_USER);
  }

  @Override public boolean onCreate() {
    return false;
  }

  @Nullable @Override
  public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s,
      @Nullable String[] strings1, @Nullable String s1) {

    final SQLiteDatabase db = ReminderDaoImpl.getInstance().getDatabase(getContext());
    final int code = uriMatcher.match(uri);
    switch (code) {
      case CODE_NOTES :
       return db.query(NOTES,strings,s,strings1,s1,null,null);
      case CODE_NOTESJOINUSER :
        return db.query(NOTES_JOIN_USER,strings,s,strings1,s1,null,null);
      case CODE_USER :
        return db.query(USER,strings,s,strings1,s1,null,null);
    }

    return null;
  }

  @Nullable @Override public String getType(@NonNull Uri uri) {
    return null;
  }

  @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
    return null;
  }

  @Override public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {

    final SQLiteDatabase db = ReminderDaoImpl.getInstance().getDatabase(getContext());
    final int code = uriMatcher.match(uri);
    switch (code) {
      case CODE_NOTES :
        return db.delete(NOTES,s,strings);
      case CODE_NOTESJOINUSER :
        return db.delete(NOTES_JOIN_USER,s,strings);
      case CODE_USER :
        return db.delete(USER,s,strings);
    }
    return 0;
  }

  @Override
  public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s,
      @Nullable String[] strings) {
    final SQLiteDatabase db = ReminderDaoImpl.getInstance().getDatabase(getContext());
    final int code = uriMatcher.match(uri);
    switch (code) {
      case CODE_NOTES :
        return db.update(NOTES,contentValues,s,strings);
      case CODE_NOTESJOINUSER :
        return db.update(NOTES_JOIN_USER,contentValues,s,strings);
      case CODE_USER :
        return db.update(USER,contentValues,s,strings);
    }

    return 0;
  }
}
